import AD from "../../common/ad.js"
import Globalunit from "../../common/globalunit.js";
export default {
    data() {
        return {
            height:0,
            coin: 0,
            money: 0,
            checkindex: 0,
            list: [],
            jianglilist: [],
            list1: [],
            toasttitle: '',
            showtoastdialog: false,
            uid: "",
            token: "",
            percent:0,
            needsong:0,
            tixiancaidui:0,
            taskinfo:{
                'coin':0,
                'type':'元'
            },
            leijicaiduigequ:0,
            firstjiangli:0,
            videofanbei:{
                "min":1,
                "max":5
            },
            todayseevideo:0,
            needvido:0,
            timer:null,
            lastvideotime: 0,
            videobutton:null
        }
    },
    onLoad() {
        var self = this
        uni.getSystemInfo({
        	//成功获取的回调函数，返回值为系统信息
        	success: (sysinfo) => {
        		var height = sysinfo.statusBarHeight
        		if(height < 44){
        		    height = 22
        		}
        		self.height = height + 'px'
        	},
        	complete: () => {}
        });
        // 可选预加载数据
        AD.load(Globalunit.adpid)
        uni.getStorage({
            key: 'videotime',
            success: function(res) {
                console.log('videotime');
                console.log(res.data);
                self.lastvideotime = Number(res.data)
            }
        });
        if (!self.timer) {
            self.timer = setInterval(function() {
                var nowtime = Math.floor(Date.now() / 1000)
                if(self.lastvideotime == 0){
                    self.videobutton = false
                }else{
                    var time = self.lastvideotime+120 - nowtime
                    self.videobutton = self.gettime(time)
                }
            }, 1000)
        }
    },
    onReady() {
        var self = this
        uni.getStorage({
            key: 'uni_id_token',
            success: function(res) {
                self.token = res.data
            }
        });
        uni.getStorage({
            key: 'uid',
            success: function(res) {
                self.uid = res.data
                self.gettaskinfo(self.uid, self.token)
            }
        });
    },
    onShow() {
        var self = this
        uni.getStorage({
            key: 'uni_id_token',
            success: function(res) {
                console.log(res.data);
                self.token = res.data
            }
        });
        uni.getStorage({
            key: 'uid',
            success: function(res) {
                self.uid = res.data
                self.gettaskinfo(self.uid, self.token)
            }
        });
    },
    methods: {
        gettime(value){
            if (!value) return '';
            if (value < 0) {
                return false
            }
            let hh = parseInt(value / 3600); //小时
            let shh = value - hh * 3600;
            let ii = parseInt(shh / 60);
            let ss = shh - ii * 60;
            return (ii < 10 ? '0' + ii : ii) + ':' + (ss < 10 ? '0' + ss : ss);
        },
        
        checktab(index) {
            var self = this
            self.checkindex = index
            self.gettaskinfo(self.uid, self.token)
        },
        gomine() {
            uni.reLaunch({
                url: '/pages/mine/mine',
            });
        },
        
        qiandaojiangli() {
            var self = this
            uni.showLoading({
                title: '处理中...'
            })
            uniCloud.callFunction({
                name: 'hallctrl',
                data: {
                    action: "hall/sign",
                    data: {
                        uid: self.uid,
                        uniIdToken: self.token,
                    }
                }
            }).then((res) => {
                uni.hideLoading()
                console.log(res)
                console.log('ressssssssssssssss', res)
                if (res.result.code && res.result.code != 0) {
                    uni.showToast({
                        title: res.result.message,
                        icon: 'none',
                    });
                    return
                }
                if (res.result.errcode && res.result.errcode == 10014) {
                    self.showtoast('未到领奖时间，请稍后');
                    return
                }
                if (res.result.errcode && res.result.errcode != 0) {
                    uni.showToast({
                        title: res.result.errmsg,
                        icon: 'none',
                    });
                    return
                }
                uni.showToast({
                    title: '领取成功',
                    icon: 'none',
                });
                self.coin = res.result.data.user.coin
                self.money = res.result.data.user.fen
                self.jianglilist = res.result.data.jianglilist
                self.tixiancaidui = res.result.data.user.tixiancaidui
                var jindu = 0;
                for(var i =0;i<self.jianglilist.length;i++){
                    if(self.jianglilist[i]['status']==3 || self.jianglilist[i]['status']==1){
                        jindu += 1
                    }
                }
                self.percent = jindu*25
            }).catch((err) => {
                console.log("err: ", err);
                uni.hideLoading()
                uni.showToast({
                    title: "获取失败，请稍后重试!",
                    icon: 'none',
                });
            })
        },
        showtoast(title) {
            var self = this
            console.log(1111)
            self.toasttitle = title
            self.showtoastdialog = true
            setTimeout(function() {
                self.showtoastdialog = false
            }, 3000)
        },
        gettaskinfo(uid, token) {
            var self = this
            if(self.coin==0){
                uni.showLoading({
                    title: '处理中...'
                })
            }
            uniCloud.callFunction({
                name: 'hallctrl',
                data: {
                    action: "hall/gettaskinfo",
                    data: {
                        uid: uid,
                        uniIdToken: token,
                        type: self.checkindex
                    }
                }
            }).then((res) => {
                uni.hideLoading()
                if (res.result.code && res.result.code != 0) {
                    uni.showToast({
                        title: res.result.message,
                        icon: 'none',
                    });
                    return
                }
                if (res.result.errcode && res.result.errcode != 0) {
                    uni.showToast({
                        title: res.result.errmsg,
                        icon: 'none',
                    });
                    return
                }
                self.coin = res.result.data.user.coin
                self.money = res.result.data.user.fen
                self.firstjiangli = res.result.data.user.firstjiangli
                
                if(res.result.data.user.leijicaiduigequ > 5){
                    self.leijicaiduigequ = 5
                }else{
                    self.leijicaiduigequ = res.result.data.user.leijicaiduigequ
                }
                self.needsong = res.result.data.needsong
                self.tixiancaidui = res.result.data.user.tixiancaidui
                if(self.checkindex == 0){
                    self.list = res.result.data.list
                }else{
                    self.list1 = res.result.data.list
                    self.jianglilist = res.result.data.jianglilist
                    var jindu = 0;
                    for(var i =0;i<self.jianglilist.length;i++){
                        if(self.jianglilist[i]['status']==3 || self.jianglilist[i]['status']==1){
                            jindu += 1
                        }
                    }
                    self.percent = jindu*25
                }
                self.videofanbei = res.result.data.videofanbei
                self.needvido = res.result.data.needvido
                self.todayseevideo = res.result.data.todayseevideo
            }).catch((err) => {
                uni.hideLoading()
                uni.showToast({
                    title: "获取失败，请稍后重试!",
                    icon: 'none',
                });
            })
        },
        closepopup(type) {
            var self = this
            if(type==0){
                self.$refs.popup.close()
            }else{
                self.showAd(0)
            }
        },
        lingqujiangli(item, index) {
            var self = this
            // self.$refs.popup.open()
            uni.showLoading({
                title: '处理中...'
            })
            uniCloud.callFunction({
                name: 'hallctrl',
                data: {
                    action: "hall/taskjiangli",
                    data: {
                        uid: self.uid,
                        uniIdToken: self.token,
                        id: item._id
                    }
                }
            }).then((res) => {
                uni.hideLoading()
                console.log('ressssssssssssssss', res)
                if (res.result.code && res.result.code != 0) {
                    uni.showToast({
                        title: res.result.message,
                        icon: 'none',
                    });
                    return
                }
                if (res.result.errcode && res.result.errcode != 0) {
                    uni.showToast({
                        title: res.result.errmsg,
                        icon: 'none',
                    });
                    return
                }
                self.coin = res.result.data.user.coin
                self.money = res.result.data.user.fen
                self.tixiancaidui = res.result.data.user.tixiancaidui
                if(self.checkindex==0){
                    self.list[index]['status'] = 2
                }else{
                    self.list1[index]['status'] = 2
                }
                if(res.result.data.type == 'coin'){
                    self.taskinfo.type = '金币'
                }else{
                    self.taskinfo.type = '元'
                }
                self.taskinfo.coin = res.result.data.coin
                self.$refs.popup.open()
            }).catch((err) => {
                console.log("err: ", err);
                uni.hideLoading()
                uni.showToast({
                    title: "领取失败，请稍后重试!",
                    icon: 'none',
                });
            })
        },
        showAd(index) {
            var self = this
            uni.showLoading({
                title: '处理中...'
            })
            uniCloud.callFunction({
                name: 'hallctrl',
                data: {
                    action: "hall/getvideocount",
                    data: {
                        uid: self.uid,
                        uniIdToken: self.token,
                    }
                }
            }).then((res) => {
                uni.hideLoading()
                self.$refs.popup.close()
                if (res.result.code && res.result.code != 0) {
                    uni.showToast({
                        title: res.result.message,
                        icon: 'none',
                    });
                    return
                }
                if (res.result.errcode && res.result.errcode != 0) {
                    uni.showToast({
                        title: res.result.errmsg,
                        icon: 'none',
                    });
                    return
                }
                AD.show(Globalunit.adpid, (res) => {
                    console.log("onclose")
                    // 用户点击了【关闭广告】按钮
                    if (res && res.isEnded) {
                        // 正常播放结束
                        console.log("onClose " + res.isEnded);
                        // 1猜对 奖励3-5倍  2 猜错复活  3 红包3-5倍
                        self.adcallback(index)
                    } else {
                        // 播放中途退出
                        console.log("onClose " + res.isEnded);
                    }
                    // 可选预加载下一条广告数据，减少加载等待时间，调用此 API 不会显示loading，不影响业务
                    AD.load(Globalunit.adpid)
                }, (err) => {
                    // 广告无法显示，输出错误信息
                    console.log(err) // {code: code, errMsg: message}
                    uni.showToast({
                        title: '广告播放错误',
                        icon: 'none',
                    });
                })
            }).catch((err) => {
                console.log("err: ", err);
                uni.hideLoading()
            })
        
        },
        adcallback(index) {
            var self = this
            uni.showLoading({
                title: '处理中...'
            })
            uniCloud.callFunction({
                name: 'hallctrl',
                data: {
                    action: "hall/videocallback",
                    data: {
                        uid: self.uid,
                        uniIdToken: self.token,
                        type: index==0?'taskjiangli':'video'
                    }
                }
            }).then((res) => {
                uni.hideLoading()
                if (res.result.code && res.result.code != 0) {
                    uni.showToast({
                        title: res.result.message,
                        icon: 'none',
                    });
                    return
                }
                if (res.result.errcode && res.result.errcode != 0) {
                    uni.showToast({
                        title: res.result.errmsg,
                        icon: 'none',
                    });
                    return
                }
                self.coin = res.result.data.user.coin
                self.money = res.result.data.user.fen
                if(index!=0){
                    var nowtime = Math.floor(Date.now() / 1000)
                    try {
                        uni.setStorageSync('videotime', nowtime);
                    } catch (e) {
                        // error
                    }
                    self.lastvideotime = nowtime
                    self.videobutton = self.gettime(120)
                }else{
                    var title = '恭喜获得' + res.result.data.beishu + '倍奖励，' + res.result.data.num
                    if(res.result.data.jianglitype == 'coin'){
                        title = title + '金币'
                    }else{
                        title = title + '元'
                    }
                    uni.showToast({
                        title: title,
                        icon: 'none',
                    });
                }
            }).catch((err) => {
                console.log("err: ", err);
                uni.hideLoading()
            })
        
        },
    }
}
